﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using System.Drawing.Imaging;
namespace Helper_View
{
    public static class View
    {
        /// <summary>
        /// 在网页上展示Bitmap图片
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public static void View_Bitmap(Bitmap[] list)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(GetHtmlHead(""));
            foreach (Bitmap map in list)
            {
                sb.Append("<p><img src=\"" + Bitmap2Base64(map) + "\" /></p>");
                sb.Append("<ol>");
                for (int h = 0; h < map.Height + 1; h++)
                {
                    sb.Append("<li>");
                    for (int w = 0; w < map.Width; w++)
                    {
                        if (h == 0)//在第一行输出列号
                        {
                            sb.Append("<span style=\"display:inline-block;width:10px;\">" + w + "</span>");
                            continue;
                        }
                        Color c = map.GetPixel(w, h - 1);
                        string name = ColorTranslator.ToHtml(c); ;
                        sb.Append("<span style=\"color:" + name + "\">■</span>");
                    }
                    sb.Append("</li>" + Environment.NewLine);
                }
                sb.Append("</ol><br/>");
            }
            sb.Append(GetHtmlFoot());
            DoView(sb.ToString());
        }
        /// <summary>
        /// 在网页上展示Bitmap图片
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public static void View_Bitmap(List<Bitmap> list)
        {
            View_Bitmap(list.ToArray());
        }
        /// <summary>
        /// 在网页上展示Bitmap图片
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public static void View_Bitmap(Bitmap map)
        {
            Bitmap[] list = new Bitmap[] { map};
            View_Bitmap(list);
        }
        private static string Bitmap2Base64(Bitmap bmp)
        {
            MemoryStream m = new MemoryStream();
            bmp.Save(m,ImageFormat.Bmp);
            byte[] b = m.GetBuffer();
            return "data:image/jpeg;base64," + Convert.ToBase64String(b);
        }
        /// <summary>
        /// 根据html字符串生成文件，并在浏览器中打开。
        /// </summary>
        /// <param name="htmlStr"></param>
        private static void DoView(string htmlStr)
        {
            string fileName = Guid.NewGuid().ToString() + ".html";
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\html\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            Common.FileHelper.WriteFile(path + fileName, htmlStr);
            Process.Start(path + fileName);
        }
        private static string GetHtmlHead(string title)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
            sb.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            sb.Append("<head>");
            sb.Append("<title>" + title + "</title>");
            sb.Append("<style type=\"text/css\">");
            sb.Append("body{font-size:12px;letter-spacing:-2px;background-color:#ccc;}");
            sb.Append("li{line-height:10px;}");
            sb.Append("</style>");
            sb.Append("</head>");
            sb.Append("<body>");
            return sb.ToString();
        }

        private static string GetHtmlFoot()
        {
            StringBuilder sb = new StringBuilder("</body></html>");
            return sb.ToString();
        }
    }
}
